{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import torch\n",
    "import os\n",
    "import glob\n",
    "from PIL import Image\n",
    "import cv2\n",
    "import numpy as np"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "class MICCAIDataSet:\n",
    "\n",
    "    def __init__(self, file_path: str, mode='train', transform=None):\n",
    "        \"\"\"\n",
    "            file_path (str): 数据集路径\n",
    "            mode (str): train, val, test\n",
    "            transform: 数据增强\n",
    "        \"\"\"\n",
    "        if mode == 'train':\n",
    "            self.img_path = glob.glob(os.path.join(file_path, 'TrainDataset/images/*.tif'))\n",
    "            self.mask_path = glob.glob(os.path.join(file_path, 'TrainDataset/masks/*.tif'))\n",
    "        elif mode == 'val':\n",
    "            self.img_path = glob.glob(os.path.join(file_path, 'ValidationDataset/images/*.tif'))\n",
    "            self.mask_path = glob.glob(os.path.join(file_path, 'ValidationDataset/masks/*.tif'))\n",
    "        else:\n",
    "            self.img_path = glob.glob(os.path.join(file_path, 'TestDataset/images/*.tif'))\n",
    "            self.mask_path = glob.glob(os.path.join(file_path, 'TestDataset/masks/*.tif'))\n",
    "        self.transform = transform\n",
    "        self.mode = mode\n",
    "\n",
    "    def __len__(self):\n",
    "        return len(self.img_path)\n",
    "\n",
    "    def __getitem__(self, idx):\n",
    "        img = self.img_path[idx]\n",
    "        print(img)\n",
    "        mask = self.mask_path[idx]       \n",
    "        img = cv2.imread(img)\n",
    "        mask = cv2.imread(mask)\n",
    "        img = cv2.resize(img, (512, 512))\n",
    "        mask = cv2.resize(mask, (512, 512))\n",
    "        if mask.max() > 1:\n",
    "            mask = np.where(mask > 0, 255, 0)\n",
    "            # mask = mask / 255\n",
    "        if self.mode == 'train':\n",
    "            name1 = f'../data/MICCAI2018MoNuSeg/TrainDataset/images512/{idx:03d}.jpg'\n",
    "            name2 = f'../data/MICCAI2018MoNuSeg/TrainDataset/masks512/{idx:03d}.jpg'\n",
    "        elif self.mode == 'val':\n",
    "            name1 = f'../data/MICCAI2018MoNuSeg/ValidationDataset/images512/{idx:03d}.jpg'\n",
    "            name2 = f'../data/MICCAI2018MoNuSeg/ValidationDataset/masks512/{idx:03d}.jpg'\n",
    "        else:\n",
    "            name1 = f'../data/MICCAI2018MoNuSeg/TestDataset/images512/{idx:03d}.jpg'\n",
    "            name2 = f'../data/MICCAI2018MoNuSeg/TestDataset/masks512/{idx:03d}.jpg'\n",
    "        cv2.imwrite(name1, img)\n",
    "        cv2.imwrite(name2, mask)\n",
    "        # img = img.transpose([2, 0, 1])     \n",
    "        # img, mask, hv_map = torch.from_numpy(img), torch.from_numpy(mask).unsqueeze(0), torch.from_numpy(hv_map)\n",
    "        return None\n",
    "        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "../data/MICCAI2018MoNuSeg/TestDataset/images/TCGA-IZ-8196-01A-01-BS1.tif\n",
      "0\n",
      "../data/MICCAI2018MoNuSeg/TestDataset/images/TCGA-CU-A0YN-01A-02-BSB.tif\n",
      "1\n",
      "../data/MICCAI2018MoNuSeg/TestDataset/images/TCGA-AC-A2FO-01A-01-TS1.tif\n",
      "2\n",
      "../data/MICCAI2018MoNuSeg/TestDataset/images/TCGA-AO-A0J2-01A-01-BSA.tif\n",
      "3\n",
      "../data/MICCAI2018MoNuSeg/TestDataset/images/TCGA-EJ-A46H-01A-03-TSC.tif\n",
      "4\n",
      "../data/MICCAI2018MoNuSeg/TestDataset/images/TCGA-HC-7209-01A-01-TS1.tif\n",
      "5\n",
      "../data/MICCAI2018MoNuSeg/TestDataset/images/TCGA-HT-8564-01Z-00-DX1.tif\n",
      "6\n",
      "../data/MICCAI2018MoNuSeg/TestDataset/images/TCGA-GL-6846-01A-01-BS1.tif\n",
      "7\n",
      "../data/MICCAI2018MoNuSeg/TestDataset/images/TCGA-FG-A4MU-01B-01-TS1.tif\n",
      "8\n",
      "../data/MICCAI2018MoNuSeg/TestDataset/images/TCGA-A6-6782-01A-01-BS1.tif\n",
      "9\n"
     ]
    }
   ],
   "source": [
    "train_dataset = MICCAIDataSet('../data/MICCAI2018MoNuSeg', mode='test')\n",
    "for idx, data in enumerate(train_dataset):\n",
    "    print(idx)\n",
    "    # break"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "py38",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.18"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
